import * as types from '../mutation-types'
// import {loginHandle, userPermission, adminHandle} from 'router/router-utils'
// import {logoutStorage} from 'api/storage'
import login from '../../api/login-manager'
import router from '../../router'
const state = {
  result: [],
  logoutResult: [],
  routerFlag: false,
  expireResult: 1
}

const getters = {
  loginError: state => state.result,
  root: state => state.expireResult
}

const actions = {
  login ({dispatch, commit}, params) {
    login.login(params)
      .then(data => {
        let wls = window.localStorage
        wls.setItem('hasLogin', 318)
        wls.setItem('root', data.user.permission)
        wls.setItem('token', data.data.data)
        let date = new Date()
        date.setTime(date.getTime() + 2 * 60 * 60 * 1000)
        document.cookie = 'token=' + data.data.data + ';expires=' + date.toGMTString()
        commit(types.LOGIN_ROOT, {root: data.user.permission})
        router.push('/EditNews')
      })
      .catch(err => {
        commit(types.ERR_LOGIN, err)
      })
  }
}

const mutations = {
  [types.ERR_LOGIN] (state, errorMsg) {
    state.result.push({isSuccess: false, message: errorMsg})
  },
  [types.LOGIN_ROOT] (state, {root}) {
    state.expireResult = root
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
